<?php 

/**
 * Class Debug
 * 
 * <example>
 * $model = new DM_Db_Model();
 * $string = $model->name;
 * DM_Debug::get()->enable( true );
 * DM_Debug::get()->dump( $model );
 * DM_Debug::get()->dump( $string );
 * DM_Debug::get()->addMessage( '--- complete ---' );
 * DM_Debug::get()->printAll();
 * DM_Debug::get()->enable( false );
 * </example>
 */

class DM_Debug {
	public $sqlList = array();
	public $errorList = array();
	public $messageList = array();
	public $dumpList = array();
	
	protected $elapse = 0; // start time
	
	protected static $instance = null;
	protected $enabled = true;
	
	protected static $copy = 0;
	public $myCopy = -1;
	
	protected function __construct() {
		$this->enabled = true;
		self::$instance = $this;
		$this->elapse = DM_Time::microtime_float();
	}
	
	// Enable or disable debug
	public function enable( $enable = true ) {
		$this->enabled = $enable;
	}
	
	// Add SQL string message
	public function addSql( $text, $numRows = "N/A", $duration = "N/A" ) {
		$this->sqlList[] = 
			'<tr><td>'.(count($this->sqlList)+1).'. '.htmlspecialchars($text).'</td>'.
			'<td>'.$numRows.'</td><td>'.$duration.'</td></tr>';
	}
	
	// Add Error string message
	public function addError( $text ) {
		$this->errorList[] = (count($this->errorList)+1).'. '.htmlspecialchars($text).'<br/><br/>';
	}
	
	// Add simple message
	public function addMessage( $text ) {
		$this->messageList[] = (count($this->messageList)+1).'. '.htmlspecialchars($text).'<br/><br/>';
	}
	
	// Add object for dump
	public function dump( &$object, $name = null ) {
		$this->dumpList[] = array( 'object' => $object, 'name' => $name );
	}
	
	// Print all debug log to the screen
	public function printAll() {
		if ( !$this->enabled ) return;
		
		echo '<html><body><br/><div style="font-size:12px; font-family:arial">';
		if ( count($this->errorList) ) {
			echo '<strong style="color:red">Errors:</strong><br/>';
			foreach( $this->errorList as &$string ) {
				echo $string;
			}
			echo '<br/>';
		}
		
		if ( count($this->messageList) ) {
			echo '<strong style="color:green">Messages:</strong><br/>';
			foreach( $this->messageList as &$string ) {
				echo $string;
			}
			echo '<br/>';
		}
		
		if ( count($this->sqlList) ) {
		
			$strongList = array(
				'SELECT',
				'WHERE',
				'ORDER BY',
				'FROM',
				'GROUP BY',
				'HAVING',
				'LIMIT',
				'OFFSET',
				'AND',
				'OR',
				'NOT',
				'UPDATE',
				'INSERT INTO',
				'DUPLICATE KEY',
				'ON',
				'VALUES',
			);
		
			echo '<strong style="color:black">SQL:</strong><br/><table style="font-size:12px">'.
				 '<tr><th>Sql query</th><th>Return rows</th><th>Elapse</th></tr>';
			foreach( $this->sqlList as &$string ) {
				foreach( $strongList as $strongText ) {
					$string = str_replace( $strongText.' ', '<strong>'.$strongText.' </strong>', $string);
				}
				echo $string;
			}
			echo '</table><br/>';
		}
		
		if ( count($this->dumpList) ) {
			echo '<strong style="color:blue">Data dumps:</strong><br/>';
			$i = 1;
			foreach( $this->dumpList as &$item ) {				
				echo $i++.'.'.($item['name']?'<strong>'.$item['name'].'</strong>':'').'<br/>';
				var_dump( $item['object'] );
				echo '<br/>';
				echo '<br/>';
			}
			echo '<br/>';
		}
		
		$elapse = DM_Time::microtime_float() - $this->elapse;
		echo 'Render time: '.$elapse.' s';
		
		echo '</div></body></html>';
	}
	
	// Get DM_Debug instance
	public static function get() {
		if ( !self::$instance ) {
			return new DM_Debug();
		}
		return self::$instance;
	}
}

?>